package com.zyx.lock.factory;

import com.zyx.lock.utils.DistributedRedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * <p>分布式锁工厂类</p>
 *
 * @author Yaxi.Zhang
 * @since 2022/9/10 10:33
 */
@Component
public class DistributedLockFactory {

    private final StringRedisTemplate redisTemplate;
    private final ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private final String uuid;

    @Autowired
    public DistributedLockFactory(StringRedisTemplate redisTemplate, ThreadPoolTaskScheduler threadPoolTaskScheduler) {
        this.redisTemplate = redisTemplate;
        this.threadPoolTaskScheduler = threadPoolTaskScheduler;
        this.uuid = UUID.randomUUID().toString();
    }

    public DistributedRedisLock getRedisLock(String lockName) {
        System.out.println(Thread.currentThread().getId());
        return new DistributedRedisLock(redisTemplate, lockName, uuid + ":" +
                Thread.currentThread().getId(), threadPoolTaskScheduler);
    }

}
